﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace siguo_shrx.server
{
    public partial class MonitorCtrl : UserControl
    {
        //常量，表示单个矩形的宽度和高度
        private const int REC_WIDTH = CEll_WIDTH * 5 + 5;//六条1像素的线五个15像素的格
        private const int REC_HEIGHT = CEll_WIDTH * 5 + 5;//六条1像素的线五个15像素的格

        //每个正方形格子的边长
        private const int CEll_WIDTH = 15;
        //第一个矩形起点
        private Point RECstartPoint = new Point((10 + REC_WIDTH/2), 10);

        private const string NAME = "司军师师旅旅团团营营连连连排排排兵兵兵炸炸雷雷雷旗";

        public MonitorCtrl()
        {
            InitializeComponent();
            //指派MonitorCtrl_Paint 方法作为MonitorCtrl控件 的Paint 事件的事件处理程序 
            this.Paint += new PaintEventHandler(MonitorCtrl_Paint);

            this.MouseClick += new MouseEventHandler(MonitorCtrl_MouseClick);
            this.btnReSet.Click += new System.EventHandler(ReSet);
        }

        private void MonitorCtrl_MouseClick(object sender, MouseEventArgs e)
        {
            //右击
            if (e.Button == MouseButtons.Right)
            {
                int sit;
                int x = e.X;
                int y = e.Y;
                if ((y - 10 > 0 && y - 10 - REC_HEIGHT < 0) && 
                    (x - RECstartPoint.X >= 0 && x - RECstartPoint.X - REC_WIDTH <= 0))//上方
                {
                    int x1 = (x - RECstartPoint.X) / (CEll_WIDTH + 1);
                    int y1 = (y - 10) / (CEll_WIDTH + 1);
                    Point p = new Point(x1,y1);
                    sit = 2;
                    CellMark(p, sit);
                }
                else if (y - 11 - REC_HEIGHT > 0 && y - 11 - 2 * REC_HEIGHT < 0)
                {
                    if (x - 10 > 0 && x -10- REC_WIDTH < 0)//左方
                    {
                        int x1 = (x - 10) / (CEll_WIDTH + 1);
                        int y1 = (y - 12 - REC_HEIGHT) / (CEll_WIDTH + 1);
                        Point p = new Point(x1, y1);
                        sit = 3;
                        CellMark(p, sit);
                    }
                    else if (x - 12 - REC_WIDTH > 0 && x - 12 - 2 * REC_HEIGHT < 0)//右方。
                    {
                        int x1 = (x - 12 - REC_WIDTH) / (CEll_WIDTH + 1);
                        int y1 = (y - 12 - REC_HEIGHT) / (CEll_WIDTH + 1);
                        Point p = new Point(x1, y1);
                        sit = 1;
                        CellMark(p, sit);
                    }
 
                }
 
            }
        }

        public void MonitorCtrl_Paint(object sender, PaintEventArgs e)
        {
            int[] arr1 = new int[25];//对家
            int[] arr2 = new int [25];//下家
            int[] arr3 = new int[25];//上家
            for (int i=0;i<25;i++)
            {
                arr1[i] = Mainfrm.record[(Mainfrm.mysitColor + 2) % 4, i];//对家
                arr2[i] = Mainfrm.record[(Mainfrm.mysitColor + 3) % 4, i];//下家
                arr3[i] = Mainfrm.record[(Mainfrm.mysitColor + 1) % 4, i];//上家
            }
            RectInit(arr1, (Mainfrm.mysitColor + 2) % 4, RECstartPoint);//上
            RectInit(arr2, (Mainfrm.mysitColor + 3) % 4, new Point(12 + REC_WIDTH, 12 + REC_HEIGHT));//右面
            RectInit(arr3, (Mainfrm.mysitColor + 1) % 4, new Point(10, 12 + REC_HEIGHT));//左面
        }

        public void mark()
        { 

        }

        //根据颜色及变化的位置，检测状态画出对应格子。
        public static void Paint_cell(int color, int index)
        {
 
        }

        //画单个矩形并初始化
        public void RectInit(int[] stat, int sit, Point point)
        {
            Pen pen;
            switch (sit)
            {
                case 0:
                    pen = new Pen(Color.Red);
                    break;
                case 1:
                    pen = new Pen(Color.Blue);
                    break;
                case 2:
                    pen = new Pen(Color.Green);
                    break;
                case 3:
                    pen = new Pen(Color.Purple);
                    break;
                default:
                    pen = new Pen(Color.Black);
                    break;
            }
            Graphics g;
            g = this.CreateGraphics();

            //SolidBrush brush = new SolidBrush(Color.GreenYellow);
            
            ////Point p = new Point(10, 20);
            //Size s = new Size(REC_WIDTH, REC_HEIGHT);
            //Rectangle rec = new Rectangle(point, s);
            //g.FillRectangle(brush,rec);


           // g.DrawRectangle(pen, rec);

            //画格子
            for (int x = point.X; x <= REC_WIDTH + point.X; x += CEll_WIDTH+1)
            {
                Point point2 = new Point(x, point.Y);
                Point point3 = new Point(x, point.Y + REC_WIDTH);
                g.DrawLine(pen, point2, point3);
            }

            for (int y = point.Y; y <= REC_HEIGHT + point.Y; y += CEll_WIDTH+1)
            {
                Point point2 = new Point(point.X, y);
                Point point3 = new Point(point.X + REC_HEIGHT, y);
                g.DrawLine(pen, point2, point3);
            }

            ////写文字
            //WriteTXTForAllCell(g,point);
            
            //填颜色
            if (stat[24] == 0)
            {
                for (int y = 0; y < 5; y++)
                {
                    for (int x = 0; x < 5; x++)
                        fillColor(new Point(point.X + x * CEll_WIDTH + x + 1, point.Y + y * CEll_WIDTH + y + 1), stat[x + y * 5], x + y * 5);
                }
            }
            else
            {
                int[] temparr = new int[25];
                for (int i = 0; i < 25; i++)
                    temparr[i] = 2;//All die!
                for (int y = 0; y < 5; y++)
                {
                    for (int x = 0; x < 5; x++)
                        fillColor(new Point(point.X + x * CEll_WIDTH + x + 1, point.Y + y * CEll_WIDTH + y + 1), temparr[x + y * 5], x + y * 5);
                }

            }
          
            pen.Dispose();
            //brush.Dispose();
            g.Dispose();
        }


        //private void FillColorForOneCell(Point point)
        //{
        //    Graphics g;
        //    g = this.CreateGraphics();

        //    SolidBrush brush = new SolidBrush(Color.GreenYellow);

        //    //Point p = new Point(10, 20);
        //    Size s = new Size(CEll_WIDTH, CEll_WIDTH);
        //    Rectangle rec = new Rectangle(point, s);
        //    g.FillRectangle(brush, rec);
        //    brush.Dispose();
        //    g.Dispose();
        //}

        //在格子上显示文本，以说明格子显示的事物
        //private void WriteTXTForAllCell(Point point)
        //{
        //    Font f = SystemFonts.CaptionFont;
        //    Graphics g = this.CreateGraphics();
        //    StringFormat sf = new StringFormat();
        //    sf.Alignment = StringAlignment.Near;
        //    sf.LineAlignment = StringAlignment.Near;
            
        //    for (int y = 0; y < 5; y++)
        //    {
        //        for(int x=0;x<5;x++)
        //        {
        //            g.DrawString(NAME[y * 5 + x].ToString(), f, Brushes.White, point.X + x * CEll_WIDTH + x + 1, point.Y + y * CEll_WIDTH + y + 1, sf);
        //        }
        //    }
        //    f.Dispose();
        //    g.Dispose();
        //}

        //对单个格子重写文字
        private void WriteTXTForOneCell(Point point,int index)
        {
            Font f = SystemFonts.CaptionFont;
            Graphics g = this.CreateGraphics();
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Near;
            sf.LineAlignment = StringAlignment.Near;
            g.DrawString(NAME[index].ToString(), f, Brushes.White, point.X , point.Y, sf);
            sf.Dispose();
            f.Dispose();
            g.Dispose();
        }

        //对单独格子处理
        public void CellMark(Point p,int sit)
        {
            switch (sit)
            {
                case 1://右边
                    Mainfrm.record[(Mainfrm.mysitColor + 3) % 4, p.X + p.Y * 5] = (Mainfrm.record[(Mainfrm.mysitColor + 3) % 4, p.X + p.Y * 5] +1)%3;
                    fillColor(new Point(13 + REC_WIDTH + p.X * CEll_WIDTH + p.X, 13 + REC_HEIGHT + p.Y * CEll_WIDTH + p.Y),
                        Mainfrm.record[(Mainfrm.mysitColor + 3) % 4, p.X + p.Y * 5],
                        p.X + p.Y * 5);
                    
                    break;
                case 2://上边
                    Mainfrm.record[(Mainfrm.mysitColor + 2) % 4, p.X + p.Y * 5] = (Mainfrm.record[(Mainfrm.mysitColor + 2) % 4, p.X + p.Y * 5] + 1) % 3;
                    fillColor(new Point(RECstartPoint.X+1 + p.X * CEll_WIDTH + p.X, RECstartPoint.Y+1  + p.Y * CEll_WIDTH + p.Y),
                        Mainfrm.record[(Mainfrm.mysitColor + 2) % 4, p.X + p.Y * 5],
                        p.X + p.Y * 5);
                    break;
                case 3://左边
                    Mainfrm.record[(Mainfrm.mysitColor + 1) % 4, p.X + p.Y * 5] = (Mainfrm.record[(Mainfrm.mysitColor + 1) % 4, p.X + p.Y * 5] + 1) % 3;
                    fillColor(new Point(11 + p.X * CEll_WIDTH + p.X, 13 + REC_HEIGHT + p.Y * CEll_WIDTH + p.Y),
                       Mainfrm.record[(Mainfrm.mysitColor + 1) % 4, p.X + p.Y * 5],
                       p.X + p.Y * 5);

                    //fillColor(new Point(13 + REC_WIDTH + p.X * CEll_WIDTH + p.X, 13 + REC_HEIGHT + p.Y * CEll_WIDTH + p.Y),
                    //    Mainfrm.record[(Mainfrm.mysitColor + 3) % 4, p.X + p.Y * 5],
                    //    p.X + p.Y * 5);
                    break;
            }
            //Graphics g;
            //g = this.CreateGraphics();

            //SolidBrush brush = new SolidBrush(Color.GreenYellow);

            ////Point p = new Point(10, 20);
            //Size s = new Size(REC_WIDTH, REC_HEIGHT);
            //Rectangle rec = new Rectangle(point, s);
            //g.FillRectangle(brush, rec);
 
        }

        public void AtuoFillColor(int color, int index)
        {
            if (color!=Mainfrm.mysitColor)
            {
                //刷子
                SolidBrush brush;
                switch (Mainfrm.record[color, index])//检查死亡状态dieMode
                {
                    case 1://对掉
                        brush = new SolidBrush(Color.OrangeRed);
                        break;
                    case 2://被吃
                        brush = new SolidBrush(Color.Black);
                        break;
                    case 0://正常

                    default:
                        brush = new SolidBrush(Color.LightSeaGreen);//.LightSeaGreen
                        break;
                }

                //画布
                Graphics g;
                g = this.CreateGraphics();

                //计算格子坐标
                Point Cellpoint;
                int x;
                int y;
                int Direction = (Mainfrm.mysitColor + color) % 4;//方向
                switch (Mainfrm.mysitColor)
                {
                    //当自己为红色时
                    //    2
                    // 1     3
                    //    0
                    case 0:
                        switch (color)
                        {
                            //上方 即对家
                            case 2:
                                x = RECstartPoint.X + (CEll_WIDTH + 1) * (index % 5) + 1;
                                y = RECstartPoint.Y + ((CEll_WIDTH + 1) * (index / 5)) + 1;
                                break;
                            //左方 即上家
                            case 1:
                                x = 11 + (CEll_WIDTH + 1) * (index % 5) + 1;
                                y = 13 + REC_HEIGHT + ((CEll_WIDTH + 1) * (index / 5)) + 1;
                                break;
                            //右方 即下家
                            case 3:
                            default:
                                x = 13 + REC_WIDTH + (CEll_WIDTH + 1) * (index % 5) + 1;
                                y = 13 + REC_HEIGHT + ((CEll_WIDTH + 1) * (index / 5)) + 1;
                                break;
                        }
                        break;
                    //当自己为蓝色时
                    //    3
                    // 2     0
                    //    1
                    case 1:
                        switch (color)
                        {
                            //上方 即对家
                            case 3:
                                x = RECstartPoint.X + (CEll_WIDTH + 1) * (index % 5) + 1;
                                y = RECstartPoint.Y + ((CEll_WIDTH + 1) * (index / 5)) + 1;
                                break;
                            //左方 即上家
                            case 2:
                                x = 11 + (CEll_WIDTH + 1) * (index % 5) + 1;
                                y = 13 + REC_HEIGHT + ((CEll_WIDTH + 1) * (index / 5)) + 1;
                                break;
                            //右方 即下家
                            case 0:
                            default:
                                x = 13 + REC_WIDTH + (CEll_WIDTH + 1) * (index % 5) + 1;
                                y = 13 + REC_HEIGHT + ((CEll_WIDTH + 1) * (index / 5)) + 1;
                                break;
                        }
                        break;
                    //当自己为绿色时
                    //    0
                    // 3     1
                    //    2
                    case 2:
                        switch (color)
                        {
                            //上方 即对家
                            case 0:
                                x = RECstartPoint.X + (CEll_WIDTH + 1) * (index % 5) + 1;
                                y = RECstartPoint.Y + ((CEll_WIDTH + 1) * (index / 5)) + 1;
                                break;
                            //左方 即上家
                            case 3:
                                x = 11 + (CEll_WIDTH + 1) * (index % 5) + 1;
                                y = 13 + REC_HEIGHT + ((CEll_WIDTH + 1) * (index / 5)) + 1;
                                break;
                            //右方 即下家
                            case 1:
                            default:
                                x = 13 + REC_WIDTH + (CEll_WIDTH + 1) * (index % 5) + 1;
                                y = 13 + REC_HEIGHT + ((CEll_WIDTH + 1) * (index / 5)) + 1;
                                break;
                        }
                        break;
                    //当自己为紫色时
                    //    1
                    // 0     2
                    //    3
                    case 3:
                    default:
                        switch (color)
                        {
                            //上方 即对家
                            case 1:
                                x = RECstartPoint.X + (CEll_WIDTH + 1) * (index % 5) + 1;
                                y = RECstartPoint.Y + ((CEll_WIDTH + 1) * (index / 5)) + 1;
                                break;
                            //左方 即上家
                            case 0:
                                x = 11 + (CEll_WIDTH + 1) * (index % 5) + 1;
                                y = 13 + REC_HEIGHT + ((CEll_WIDTH + 1) * (index / 5)) + 1;
                                break;
                            //右方 即下家
                            case 2:
                            default:
                                x = 13 + REC_WIDTH + (CEll_WIDTH + 1) * (index % 5) + 1;
                                y = 13 + REC_HEIGHT + ((CEll_WIDTH + 1) * (index / 5)) + 1;
                                break;
                        }
                        break;
                }
                Cellpoint = new Point(x, y);
                //Point p = new Point(10, 20);
                Size s = new Size(CEll_WIDTH, CEll_WIDTH);
                Rectangle rec = new Rectangle(Cellpoint, s);
                g.FillRectangle(brush, rec);

                brush.Dispose();
                g.Dispose();
                WriteTXTForOneCell(Cellpoint, index); 
            }
 
        }

        //根据坐标给格子换色。
        private void fillColor(Point point, int state, int index)
        {
            SolidBrush brush;
            switch (state)
            {
                case 1://对掉
                    brush = new SolidBrush(Color.OrangeRed);
                    break;
                case 2://被吃
                    brush = new SolidBrush(Color.Black);
                    break;
                case 0://正常
                    
                default:
                    brush = new SolidBrush(Color.LightSeaGreen);//.LightSeaGreen
                    break;
            }
            Graphics g;
            g = this.CreateGraphics();

            //Point p = new Point(10, 20);
            Size s = new Size(CEll_WIDTH, CEll_WIDTH);
            Rectangle rec = new Rectangle(point, s);
            g.FillRectangle(brush, rec);

            brush.Dispose();
            g.Dispose();
            WriteTXTForOneCell(point, index);
            
        }

        //重置按钮
        public void ReSet(object sender, EventArgs e)
        {
            for(int i=0;i<4;i++)
            {
                for(int j=0;j<25;j++)
                {
                    Mainfrm.record[i, j] = 0; 
                }
            }
            this.Refresh();//重绘
        }

        ////根据某颜色和对应子力进行格子绘画
        //public void CellMark(int Color, int qiziIndex)
        //{
        //    //确定对应颜色位置
        //    int sit=Color;

        //    //switch (sit)
        //    //{
 
        //    //}
 
        //}

    }
}
